package dao

import (
	"context"
	"github.com/olivere/elastic/v7"
	"strconv"
)

type User struct {
	Id       int64  `json:"id"`
	Email    string `json:"email"`
	Nickname string `json:"nickname"`
	Phone    string `json:"phone"`
}
type UserElasticDAO struct {
	client *elastic.Client
}

func (h *UserElasticDAO) InputUser(ctx context.Context, user User) error {
	_, err := h.client.Index().Index(UserIndexName).Id(strconv.FormatInt(user.Id, 10)).
		BodyJson(user).Do(ctx)
	return err
}
func (h *UserElasticDAO) SearchUser(ctx context.Context, keywords []string) ([]User, error) {
	//queryString := strings.Join(keywords, " ")

	// 内容或标题 模糊查找
	query := elastic.NewMultiMatchQuery("nickname", keywords...)

	res, err := NewSearcher[User](h.client, UserIndexName).Query(query).Do(ctx)
	if err != nil {
		return nil, err
	}
	return res, nil
}
